package com.eat.fish_service.controller.wx;

import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.User;
import com.eat.fish_service.entity.VO.AquariumIds;
import com.eat.fish_service.service.AquariumService;
import com.eat.fish_service.service.EquipmentService;
import com.eat.fish_service.service.UserAquariumService;
import com.eat.fish_service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author CZF
 * @create 2022-03-20 17:07
 */

@RestController
@RequestMapping("/wx/ucenter")
@Api(tags = "微信端接口")
@CrossOrigin
public class WxApiController {

    @Autowired
    private UserService userService;

    @Autowired
    private AquariumService aquariumService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private UserAquariumService userAquariumService;

    @GetMapping("test")
    public R callback(){
        return R.ok();
    }

    //注册
    @ApiOperation(value = "注册")
    @PostMapping("register")
    public R register(@RequestBody User user){
        return userService.register(user);
    }


    //登录
    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R loginUser(@RequestBody User user){
        System.out.println("登录接口被调用");
        return userService.login(user);
    }

    //获取用户身份
    @ApiOperation(value = "获取用户身份")
    @GetMapping("status/{username}")
    public R getStatus(@PathVariable String username){
        return userService.getStatus(username);
    }

    /**
     * 用户添加鱼缸：直接通过鱼缸id来添加进“我的水族箱”，注意与“管理员添加鱼缸区分”！
     * 如果 添加鱼缸 或 鱼缸重启，用户输入鱼缸的id，可以从数据库中查找该鱼缸的ip和port，然后连接该鱼缸
     */
    @ApiOperation(value = "用户添加鱼缸/重启鱼缸，参数1：鱼缸id 参数2：用户名,如果请求报错含有Connection refused，表示鱼缸的系统未开启或者鱼缸id错误，添加鱼缸失败")
    @GetMapping("add_or_restart/{aquariumId}/{username}")
    public R addOrRestart(@PathVariable("aquariumId") Long aquariumId,@PathVariable("username") String username){
        return aquariumService.addOrRestart(aquariumId,username);
    }

    /**
     * 用户删除鱼缸
     */
    @ApiOperation(value = "用户删除鱼缸，参数1：用户名；参数2：鱼缸id数组")
    @PostMapping("delUserAquarium/{username}")
    public R delUserAquarium(@PathVariable("username") String username,@RequestBody AquariumIds aquariumIds){
         return aquariumService.delUserAquarium(username,aquariumIds);
    }

    /**
     * 上传头像
     */
    @ApiOperation(value = "根据用户id上传用户头像")
    @PostMapping("uploadAvater/{id}")
    public R uploadAvater(@RequestBody MultipartFile pic,
                          @PathVariable("id") String id){
        return userService.uploadAvater(pic,id);
    }

}
